<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <div id="app">
        <!-- write your vue tags or common html tages there: -->
        <h1>fruitList</h1>
        <!-- test form -->
        <!-- <form  @submit.prevent="insert">
            <input type="text">
            <input type="text">
        </form> -->
        <form @submit.prevent="insert">
            <span>name</span>
            <input type="text" v-model="name">
            </br>
            <span>price:</span>
            <input type="text" v-model.number="price">
            </br>
            <button>add</button>
        </form>
        <ul>
            <li v-for="item,index in listFruit">
                name:{{item.name}};
                price:{{item.price}};
                counter:
                <button @click="decrease(index)">-</button>
                <!-- the count will be display with value: -->
                <span>
                    {{item.count}}
                </span>
                <button @click="increase(index)">+</button>
            </li>
        </ul>
        <h3>total price:{{totalPrice}}</h3>
    </div>
    <!-- import vue cdn: -->
    <script src="https://unpkg.com/vue@next"></script>
    <!-- write your js code: -->
    <script>
        Vue.createApp({
            data() {

                return {
                    // name: "test",
                    // price: 0,
                    listFruit: [
                        {
                            name: "banana",
                            price: 11,
                            count: 2
                        }
                    ]
                }

            },
            /* defined a computed property */
            computed: {
                totalPrice() {
                    let sum = 0;
                    this.listFruit.forEach(item => {
                        sum += (item.price * item.count);
                    });
                    // return 100;
                    return sum;
                }
            },
            methods: {

                /* separate your variables by comma:`,` */
                insert() {
                    this.listFruit.push({
                        name: this.name,
                        price: this.price,
                        count: 1
                    })
                },
                increase(i) {
                    this.listFruit[i].count++
                },
                decrease(i) {
                    ret = this.listFruit[i].count--
                    console.log(ret);
                    if (ret <= 0 && confirm("delete the run up item?")) {
                        this.listFruit.splice(i, 1);

                    }

                }

            }
        }).mount("#app")
    </script>
</body>

</html>